<?php


namespace app\common\library;


use think\facade\Env;

class dotgen
{
    protected $list=[];
    protected $arr=[];
    protected $arr_gener=[];
    protected $plantuml=[];
    protected $parsedGraph;


    function __construct($list)
    {
        $this->list=$list;
    }

    private function search_for_list($parent_name,$marriages){
        foreach ($marriages as $key=>$item){
            foreach ($item['children'] as $child){
                if (isset($this->arr_gener[$child['generation']])){

                }
                else{
                    $this->arr_gener[$child['generation']] = array();
                }

                array_push($this->arr_gener[$child['generation']],$child['name']);

                $tmp_arr=[];
                array_push($tmp_arr,$parent_name);
                array_push($tmp_arr,$child['name']);
                array_push($this->arr,$tmp_arr);

                if (isset($child['marriages'])){
                    $this->search_for_list($child['name'],$child['marriages']);
                }
            }

        }
    }

    function generate()
    {
        //设计结构
        $this->arr_gener[$this->list['generation']]=array();
        array_push($this->arr_gener[$this->list['generation']],$this->list['name']);

        $this->search_for_list($this->list['name'],$this->list['marriages']);

        $this->parsedGraph = "digraph ztree {\nrankdir=TB;\n{\n node[shape=plaintext,fontname=\"Microsoft YaHei\" fontsize=16];\n";
        foreach ($this->arr as $item){
            //dump($item);
            $this->parsedGraph =   $this->parsedGraph.$item[0]."->".$item[1].";\n";
        }

        $this->parsedGraph = $this->parsedGraph."}\n{\n node[shape=plaintext,fontname=\"Microsoft YaHei\" fontsize=16];\n";

        $keys = array_keys($this->arr_gener);


        for ($i=0;$i<count($keys);++$i){
            $v = $this->NumToCNMoney($keys[$i]);
            if ($i != count($keys)-1)
                $this->parsedGraph = $this->parsedGraph.$v."世->";
            else
                $this->parsedGraph = $this->parsedGraph.$v."世[style=invis]\n";
        }

        $this->parsedGraph =$this->parsedGraph."}\n}";
    }

    protected function parsedGraph_str()
    {
        return $this->parsedGraph;
    }

    protected function parsedGraph_id()
    {
        return $this->list['tree_id'];
    }

    protected  function save_dotfile()
    {
        $this->generate();
        if (!empty($this->parsedGraph)) {
            $file = Env::get('runtime_path').'/graph/'.$this->list['tree_id'].".dot";
            echo $file;
            if ($fp = @fopen($file, 'w')) {
                @fputs($fp, $this->parsedGraph, strlen($this->parsedGraph));
                @fclose($fp);

                return $file;
            }
        }

        return false;
    }

    private function search_for_plantumllist($parent_name,$marriages){
        foreach ($marriages as $key=>$item){
            foreach ($item['children'] as $child){

                if (isset($this->arr_gener[$child['generation']])){

                }
                else{
                    $this->arr_gener[$child['generation']] = array();
                }

                array_push($this->arr_gener[$child['generation']],$child['name']);

                $tmp_arr=[];

                array_push($tmp_arr,$child['name']);
                array_push($tmp_arr,$child['generation']);
                array_push($this->plantuml,$tmp_arr);

                if (isset($child['marriages'])){
                    $this->search_for_plantumllist($child['name'],$child['marriages']);
                }
            }

        }
    }

    function save_plantuml()
    {
        //设计结构
        //设计结构
        $this->arr_gener[$this->list['generation']]=array();
        array_push($this->arr_gener[$this->list['generation']],$this->list['name']);

        $this->plantuml =[];
        $tmpArr = [];

        array_push($tmpArr,$this->list['name']);
        array_push($tmpArr,$this->list['generation']);
        array_push($this->plantuml,$tmpArr);
        $this->search_for_plantumllist($this->list['name'],$this->list['marriages']);

        $this->parsedGraph = "@startmindmap\n<style>\narrow {\nLineColor black\n}\n</style>\n";

        $top_generation = $this->list['generation'];

        foreach ($this->plantuml as $val){
            $tmp_generation = $val[1];

            for ($i =$top_generation;$i<=$tmp_generation;++$i){
                $this->parsedGraph .='*';
            }

            $this->parsedGraph .= "_ $val[0]\n";
        }

        $this->parsedGraph .= "@endmindmap\n";


        if (!empty($this->parsedGraph)) {
            $file = Env::get('runtime_path').'/graph/'.$this->list['tree_id'].".txt";
            echo $file;
            if ($fp = @fopen($file, 'w')) {
                @fputs($fp, $this->parsedGraph, strlen($this->parsedGraph));
                @fclose($fp);

                return $file;
            }
        }
    }

    function generate_png()
    {
        if ($this->save_dotfile()){

            $outputfile = Env::get('runtime_path').'/graph/'.$this->list['tree_id'].".png";;

            if (substr(php_uname(), 0, 7) == "Windows") {
                $src = Env::get('runtime_path').'/graph/'.$this->list['tree_id'].".dot";
                $outputfile  = '"' . $outputfile   . '"';
            } else {
                $src = Env::get('runtime_path').'/graph/'.$this->list['tree_id'].".dot";
            }

            $command  = "dot";
            $command .= " -Tpng -o $outputfile $src";
            @`$command`;

            return true;
        }
        else{
            return false;
        }
    }

    function NumToCNMoney($num,$mode = true,$sim = true){
        if(!is_numeric($num)) return '含有非数字非小数点字符！';
        $char  = $sim ? array('零','一','二','三','四','五','六','七','八','九')
            : array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖');
        $unit  = $sim ? array('','十','百','千','','万','亿','兆')
            : array('','拾','佰','仟','','萬','億','兆');

        //整数部分
        $str = $mode ? strrev(intval($num)) : strrev($num);
        for($i = 0,$c = strlen($str);$i < $c;$i++) {
            $out[$i] = $char[$str[$i]];
            if($mode){
                $out[$i] .= $str[$i] != '0'? $unit[$i%4] : '';
                if($i>1 and $str[$i]+$str[$i-1] == 0){
                    $out[$i] = '';
                }
                if($i%4 == 0){
                    $out[$i] .= $unit[4+floor($i/4)];
                }
            }
        }

        $retval = join('',array_reverse($out));
        return $retval;
    }
}